package com.ctbu.web;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {
    //单图片上传
    @PostMapping("/singleImage")
    public String singleImage(@RequestParam("file")MultipartFile file) throws IOException {
        //获取图片名称
        String filename=file.getOriginalFilename();
        //读取后缀
        String suffix=filename.substring(filename.lastIndexOf("."));
        //新名称
        String new_filename= UUID.randomUUID()+suffix;
        //存储地址
        String path="E:\\比特就业课\\test_java\\JavaEE进阶代码\\ctbu-loan\\loan-images\\";
        File f=new File(path);
        //如果没有文件夹新建即可
        if(!f.exists()){
            f.mkdirs();
        }
        //上传
        file.transferTo(new File(path+new_filename));
        //处理
        return "http://localhost:10001/loan-images/"+new_filename;
    }
    //多图片上传
}
